package com.ruoyi.diagnosis.listener;

import com.fazecast.jSerialComm.SerialPort;
import com.fazecast.jSerialComm.SerialPortDataListener;
import com.fazecast.jSerialComm.SerialPortEvent;
import com.ruoyi.diagnosis.util.ModBusCommandUtil;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.LinkedList;
import java.util.Queue;
import java.util.TooManyListenersException;

/**
 * 串口监听器
 *
 * @author Yeats_Liao
 * @date 2023-11-17
 */
@Slf4j
@Getter
public class MySerialPortEventListener implements SerialPortDataListener {


    private final int[] rasterAddress;
    private final int conveyorAddress;

    /**
     * -- GETTER --
     *  获取串口对象
     * <a href="https://fazecast.github.io/jSerialComm/javadoc/com/fazecast/jSerialComm/package-summary.html">...</a>
     * @return 串口对象
     */
    // 事件源
    private final SerialPort serialPort;

    // 事件类型
    private int eventType;


    /**
     * 构造串口监听器
     *
     * @param serialPort
     * @param eventType
     * @throws TooManyListenersException
     */
    public MySerialPortEventListener(SerialPort serialPort, int eventType,int[] rasterAddress,int conveyorAddress) throws TooManyListenersException {
        this.serialPort = serialPort;
        this.eventType = eventType;
        this.rasterAddress = rasterAddress;
        this.conveyorAddress = conveyorAddress;
        //给串口添加事件监听
        serialPort.addDataListener(this);
        //串口有数据监听
//        serialPort.setFlowControl(SerialPort.FLOW_CONTROL_DISABLED);
        //中断事件监听

    }

    /**
     * 串口监听事件
     *
     * @param arg0
     */

    @Override
    public void serialEvent(SerialPortEvent arg0) {
        if (arg0.getEventType() == SerialPort.LISTENING_EVENT_DATA_AVAILABLE) {
            // 数据通知
//            byte[] bytes = SerialPortTool.getDataFromPort(serialPort);
            byte[] newData = new byte[serialPort.bytesAvailable()];
            serialPort.readBytes(newData, newData.length);
            // 处理接收到的数据
            String receivedData = new String(newData);
            System.out.println("Received data: " + receivedData);
        }
    }

    @Override
    public int getListeningEvents() {
        return SerialPort.LISTENING_EVENT_DATA_AVAILABLE;
    }

    private static void processNewData(byte[] newData, int bytesRead) {
        // 创建一个队列来保存接收到的数据
        Queue<Byte> buffer = new LinkedList<>();
        for (int i = 0; i < bytesRead; i++) {
            buffer.add((byte) newData[i]);
//            System.out.println("数据保存在队列中......"+i%8);
            // 检查缓冲区是否以换行符结束
            if (buffer.size() == 8) {
//                System.out.println("开始截取一个完整命令......");
                // 复制缓冲区中的数据到数组
                byte[] fullCommand = new byte[buffer.size()];
                int index = 0;
                for (Byte b : buffer) {
                    fullCommand[index++] = b;
                }
                // 清空缓冲区
                buffer.clear();
                // 处理完整的命令
                ModBusCommandUtil.handleModbusCommand(fullCommand);
            }
        }
    }






}

